package cn.yunbamboos.rest.service.session;

import cn.yunbamboos.exception.AppException;
import cn.yunbamboos.rest.service.IRestService;
import cn.yunbamboos.rest.service.ISession;
import io.micrometer.common.util.StringUtils;
import jakarta.servlet.http.HttpServletRequest;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 会话内容
 */
public class Session implements ISession {

    /**
     * 自定义属性
     */
    private final Map<String, String> attrs = new ConcurrentHashMap<>();

    private HttpServletRequest request;

    private IRestService service;
    /**
     * 会话内容
     */
    public Session() {
    }


    @Override
    public void set(String key, String value) {
        this.attrs.put(key, value);
    }

    @Override
    public String get(String key) {
        return this.attrs.get(key);
    }

    @Override
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public HttpServletRequest getRequest() {
        return request;
    }

    public IRestService getService() {
        return service;
    }

    public void setService(IRestService service) {
        this.service = service;
    }

    @Override
    public String getRequired(String key) {
        String value = attrs.get(key);
        if (StringUtils.isEmpty(value)) {
            throw AppException.c("缺少必需的会话属性{0}", key);
        }
        return value;
    }

}
